package com.cnap.multiclouldmgmt.model.gpu;

import com.cnap.multiclouldmgmt.model.flavor.DiskTypeInfo;
import com.cnap.multiclouldmgmt.model.flavor.FlavorInfo;
import com.cnap.multiclouldmgmt.model.flavor.ImageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FlavorGPUInfoWithImageDisk {

    private static final Logger LOGGER = LoggerFactory.getLogger(FlavorGPUInfoWithImageDisk.class);


    private String accountName;

    /**
     * 云类型
     */
    private String cloudType;
    /**
     * 云区域
     */
    private String regionId;

    /**
     * 云区域描述
     */
    private String regionDescribe;

    /**
     * 规格id
     * "ecs.n1.tiny"
     */
    private String val;

    /**
     * images
     * "imageId": "aliyun_3_x64_20G_qboot_alibase_20230727.vhd",
     *  "describe": "Alibaba Cloud Linux  3.2104 LTS 64位 快速启动版"
     */
    private List<ImageInfo> images;

    /**
     * key
     * "ecs.n1.tiny[1Cores][1.0GB]"
     */
    private String key;

    /**
     * diskTypes
     */
    private List<DiskTypeInfo> diskTypes;

    /**
     * gpu信息
     */
    private GpuSpecInfo gpuSpec;

    /**
     * 可用区
     */
    private String zoneId;

    private int cpu;

    private int mem;

    //解析出cpu和mem大小，格式长这样c7.xlarge.2[4Cores][8GB]
    private void handdleCpuAndMem(){
        try {
            String cpuAndMem =
                    key.replace(val, "")
                            .replace(".0", "");
            String[] nums = cpuAndMem.split("ores");
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            //正则表达式获取数字
            for (String num : nums) {
                Matcher m = p.matcher(num);

                if (cpu == 0) {
                    cpu = Integer.parseInt(m.replaceAll("").trim());
                    continue;
                }

                mem = Integer.parseInt(m.replaceAll("").trim());
            }
        }catch (Exception e){

            LOGGER.error("handle FlavorGPUInfoWithImageDisk fail id={} describe={}",val,key);
        }
    }

    public int getCpu() {

        if (cpu == 0){
            handdleCpuAndMem();
        }


        return cpu;
    }

    public void setCpu(int cpu) {
        this.cpu = cpu;
    }

    public int getMem() {

        if (mem == 0){
            handdleCpuAndMem();
        }

        return mem;
    }

    /**
     * get
     *
     * @return ZoneId
     */
    public String getZoneId() {
        return zoneId;
    }

    /**
     * set
     *
     * @param zoneId zoneId
     */
    public void setZoneId(String zoneId) {
        this.zoneId = zoneId;
    }

    /**
     * get
     *
     * @return Val
     */
    public String getVal() {
        return val;
    }

    /**
     * set
     *
     * @param val val
     */
    public void setVal(String val) {
        this.val = val;
    }

    /**
     * get
     *
     * @return Images
     */
    public List<ImageInfo> getImages() {
        return images;
    }

    /**
     * set
     *
     * @param images images
     */
    public void setImages(List<ImageInfo> images) {
        this.images = images;
    }

    /**
     * get
     *
     * @return Key
     */
    public String getKey() {
        return key;
    }

    /**
     * set
     *
     * @param key key
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * get
     *
     * @return DiskTypes
     */
    public List<DiskTypeInfo> getDiskTypes() {
        return diskTypes;
    }

    /**
     * set
     *
     * @param diskTypes diskTypes
     */
    public void setDiskTypes(List<DiskTypeInfo> diskTypes) {
        this.diskTypes = diskTypes;
    }

    /**
     * get
     *
     * @return GpuSpec
     */
    public GpuSpecInfo getGpuSpec() {
        return gpuSpec;
    }

    /**
     * set
     *
     * @param gpuSpec gpuSpec
     */
    public void setGpuSpec(GpuSpecInfo gpuSpec) {
        this.gpuSpec = gpuSpec;
    }

    /**
     * get
     *
     * @return AccountName
     */
    public String getAccountName() {
        return accountName;
    }

    /**
     * set
     *
     * @param accountName accountName
     */
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    /**
     * get
     *
     * @return CloudType
     */
    public String getCloudType() {
        return cloudType;
    }

    /**
     * set
     *
     * @param cloudType cloudType
     */
    public void setCloudType(String cloudType) {
        this.cloudType = cloudType;
    }

    /**
     * get
     *
     * @return RegionId
     */
    public String getRegionId() {
        return regionId;
    }

    /**
     * set
     *
     * @param regionId regionId
     */
    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    /**
     * get
     *
     * @return RegionDescribe
     */
    public String getRegionDescribe() {
        return regionDescribe;
    }

    /**
     * set
     *
     * @param regionDescribe regionDescribe
     */
    public void setRegionDescribe(String regionDescribe) {
        this.regionDescribe = regionDescribe;
    }
}


